package com.gosling.gosling;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSONObject;
import com.gosling.cloudspace.tools.SnowflakeIdTool;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class SysTextSearchFileTest {

    public final static String DATE_PATTERN = "yyyy-MM-dd";
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {

        SnowflakeIdTool id = new SnowflakeIdTool(10,10);
        System.out.println(id.nextIdToString());
        System.out.println(id.nextIdToString());
        System.out.println(id.nextIdToString());
        System.out.println(id.nextIdToString());
    }

    private synchronized static String getString() {
        return "1734000.08";
    }

    private static void sdfd() {
        String json = "231,1212,2312";
        String string = JSONObject.toJSONString(json);
        System.out.println(string);

        String[] split = json.split(",");
        HashSet<String> elements = new HashSet<>(Arrays.asList(split));
        elements.add("2313123");
        String join = String.join(",", elements);
        System.out.println(join);
    }

    private static void dsfds() {
        BigDecimal bigDecimal = new BigDecimal("0.0005").multiply(new BigDecimal(100));
        System.out.println(bigDecimal.toString());
    }

    private static void dsadas() {
        DateTime dateTime = DateUtil.date();
        String yesterday = offsetDay2(dateTime.toString(), -1);
        System.out.println(yesterday);

        System.out.println(new Date().toString());

        DateTime endDate = DateUtil.offsetDay(DateUtil.parseDate("2022-10-25"), 60);
        long between = DateUtil.between(new Date(), endDate, DateUnit.WEEK);
        System.out.println(between);
    }


    public static String offsetDay2(String date, int offset) {

        DateTime dateTime = DateUtil.offsetDay(DateUtil.parseDate(date), offset);

        return DateUtil.formatDate(dateTime);
    }

    public static String NUMBER_FORMULA(String E_Value) {
        String value=null;

        // 判断是否为科学计数法（包含E、e、+等符号）
        if (("" + E_Value).indexOf("E") != -1 || ("" + E_Value).indexOf("e") != -1 || ("" + E_Value).indexOf("+") != -1) {
            BigDecimal bd = new BigDecimal("" + E_Value);
            if (("" + E_Value).indexOf("E") != -1 || ("" + E_Value).indexOf("e") != -1 || ("" + E_Value).indexOf("+") != -1) {
                bd = new BigDecimal(Double.parseDouble(bd.toString()));
                value = bd.toString();
            } else {
                value = "" + E_Value;
            }
        } else {
            value = "" + E_Value;
        }
        return value;
    }

    private static long getBetweenMinute() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000 / 60;
    }


    private static void safasfasf() {
        System.out.println(offsetDay(DateUtil.date().toString(), -1));
        System.out.println(isLegalDate("2022-09-20", DATE_PATTERN));
        System.out.println(DateUtil.date().toString());

        Assert.isTrue(isLegalDate("2022-09-209", DATE_PATTERN), "时间格式校验错误！");
    }


    /**
     * 根据时间 和时间格式 校验是否正确
     * @param sDate 校验的日期
     * @param format 校验的格式
     * @return
     */
    public static boolean isLegalDate(String sDate,String format) {
        if ((sDate == null)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    public static String offsetDay(String date, int offset) {

        DateTime dateTime = DateUtil.offsetDay(DateUtil.parseDate(date), offset);

        return DateUtil.formatDate(dateTime);
    }

    private static void loooo2(String payAccName) {
        char[] chars = payAccName.toCharArray();
        StringBuffer encript = new StringBuffer();
        StringBuffer field = new StringBuffer();
        LinkedList<String> linkedHashSet = new LinkedList<>();
        for (char aChar : chars) {
            String value = String.valueOf(aChar);
            linkedHashSet.add(value);
            encript.append(MD5.create().digestHex16(value));
        }

        int k = linkedHashSet.size() - 2;
        for (int i = 0; i < k; i++) {
            for (int m = 0; m < linkedHashSet.size(); m++) {

            }
        }


    }

    private static void loooo() {
        String payAccName = "快乐酷宝啊";
        char[] chars = payAccName.toCharArray();

        StringBuffer encript = new StringBuffer();
        StringBuffer field = new StringBuffer();

        LinkedList<String> linkedHashSet = new LinkedList<>();
        for (char aChar : chars) {
            String value = String.valueOf(aChar);
            linkedHashSet.add(value);
            encript.append(MD5.create().digestHex16(field.toString()));
        }

        for (int i = 0; i < linkedHashSet.size(); i++) {
            for (int k = 0; k < linkedHashSet.size(); k++) {
                if (Math.abs(k - i) == 1) {
                    field.append(linkedHashSet.get(i));
                    field.append(linkedHashSet.get(k));
                    encript.append(MD5.create().digestHex16(field.toString()));
                }
            }
        }
        System.out.println(encript.toString());
    }

    private static void dasoihfao() {
        String custNo2 = "1";
        String payAccName2 = "1";
        String payAccNo2 = "1";

        String custNo = "1";
        String payAccName = "2";
        String payAccNo = "1";

        StringBuilder msg = new StringBuilder("编辑失败，错误原因：");
        msg.append("客户号+付款账户名+付款账户号出现相同记录。");
        msg.append("其中：");
        if (custNo2.equals(custNo)) {
            msg.append("商户号已存在，");
        }
        if (payAccName2.equals(payAccName)) {
            msg.append("付款账户名已存在，");
        }
        if (payAccNo2.equals(payAccNo)) {
            msg.append("付款账户号已存在");
        }

        System.out.println(msg.toString());
    }

    private static void looo() {
        try {
            String retData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<moboAccount><respData><respCode>00</respCode><respDesc>成功</respDesc><result>{\"name\":\"黄夜\",\"sex\":\"男\",\"folk\":\"汉\",\"birthday\":\"1999年10月26日\",\"address\":\"成都市青羊区人民中路三段2号\",\"num\":\"522129198910263511\",\"issue\":null,\"period\":null,\"type\":\"1\"}</result></respData><signMsg>16f49e8e7c713f22855e446766828b4c</signMsg></moboAccount>";
            String s1 = "{\"retData\":\"{\\\"name\\\":\\\"黄\uE000琪\uE000\\\",\\\"sex\\\":\\\"男\uE000\\\",\\\"folk\\\":\\\"汉\uE000\\\",\\\"birthday\\\":\\\"1989年\uE00010月\uE00026日\uE000\\\",\\\"address\\\":\\\"成\uE000都\uE000市\uE000\n" +
                    "青羊区人民中路三段2号\\\",\\\"num\\\":\\\"522129198910263511\\\",\\\"issue\\\":null,\\\"period\\\":null,\\\"type\\\":\\\"1\\\"}\",\"serName\":\"COMMON\",\"source\":\"01\",\"retCode\":\"1\",\"retMsg\":\"成功\"}";
//            System.out.println(s1.replace("\\", ""));

//            Document domRoot = DocumentHelper.parseText(s);
//            Element respData = domRoot.getRootElement().element("respData");
//            String domRespMsg = respData.elementText("respDesc");
//            Element result = respData.element("result");
//
//            System.out.println(result.getStringValue());
//
//            Object parse = JSONObject.toJSON(s1);
//            System.out.println(parse);

            System.out.println(JSONObject.toJSONString(retData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void llk() {
        String currTime = "2022/09-08 15:11:32";
        System.out.println(currTime);
//        System.out.println(DateUtil.parse(currTime, "yyyy-MM-dd HH:mm:ss"));
        DateTime dateTime = DateUtil.parseDateTime(currTime);
        System.out.println(dateTime.toString());
    }

    private static void jsonStringObbb() {
        Object s = "{\"result\":{\"score\":\"88.48\"},\"msg\":\"请求成功\",\"code\":\"0\",\"fee\":1}";
//        String s = "{\"resultflow\":\"495605948237238272\",\"result\":\"1\",\"msgtype\":\"0001\",\"msg\":\"验证一致\",\"pay\":1}";
//        String s = "{\"resultflow\":\"495605948237238272\",\"result\":\"1\",\"msgtype\":\"0001\",\"msg\":\"验证一致\"}";

        JSONObject jsonObject = JSONObject.parseObject(s.toString());
        System.out.println(jsonObject.get("fee"));
    }

    public void downloadNet(String urlStr) throws MalformedURLException {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;
        URL url = new URL(urlStr);
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream("D:\\项目说明\\9、批量操作\\24、电子签约文件\\结果" + UUID.fastUUID() + ".pdf");
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}